﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WindowsFormsApplication1.Common;
using WindowsFormsApplication1.Model;
using Maticsoft.DBUtility;
using Newtonsoft.Json;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        private byte? tt;

        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //string ss = JsonConvert.SerializeObject(new {Title="1111",Name="adddd"});
            //MessageBox.Show(ss);

            //string[] toUsers = {"1678111@qq.com"};
            //Email.SendEmail("smtp.qq.com", "1678111@qq.com", ""
            //    , toUsers, "Test", "test---test", @"C:\Users\Administrator\Desktop\酒店销账 - 1.xlsx");

            //DateTime dt1 = DateTime.Parse("2017-12-31");
            //DateTime dt2 = DateTime.Parse("2017-12-04");

            //int days = HolidayHelper.GetInstance().GetWorkDayNum(dt1, dt2, false);
            //MessageBox.Show(days.ToString());

            //days = HolidayHelper.GetInstance().GetWorkDayNum(dt1, DateTime.Now, false);
            //MessageBox.Show(days.ToString());

            //DateTime dtTest;
            //if (!DateTime.TryParse("a9:00", out dtTest))
            //{
            //    dtTest = DateTime.Now;
            //}

            //MessageBox.Show(dtTest.ToString());

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (this.comboBox1.Text == "")
            {
                MessageBox.Show("请选择导入类型！");
                comboBox1.Focus();
                return;
            }
            else if(this.comboBox1.Text == "酒店")
            {
                HotelInfoImport();
            }
            else if (this.comboBox1.Text == "房型")
            {
                RoomImport();
            }
            else if (this.comboBox1.Text == "图片")
            {
                PicImport();
            }

            MessageBox.Show("执行完成！");

            #region BAK_CODE

            //DataTable dt = CSVFileHelper.OpenCSV(textBox1.Text.Trim());//@"D:\途牛对接\todev_1019途牛\todev_1019\hotel_20171019_1317_07.csv"));
            //MessageBox.Show(dt.Rows.Count.ToString());

            //if (this.listBox1.Items.Count == 0)
            //{
            //    MessageBox.Show("请选择文件！");
            //}
            //else
            //{
            //    foreach (var item in this.listBox1.Items)
            //    {
            //        this.listBox2.Items.Add("正在处理：" + item.ToString());

            //        DataTable dtRst =
            //            CsvParsingHelper.CsvToDataTable(item.ToString(), true);

            //        if (dtRst != null)
            //        {
            //            //MessageBox.Show(dtRst.Rows.Count.ToString());
            //            this.listBox2.Items.Add(item.ToString() + "包含数据：" + dtRst.Rows.Count.ToString());

            //            StringBuilder sb = new StringBuilder();

            //            if (dtRst.Columns.Count > 52)
            //            {
            //                var cCount = dtRst.Columns.Count - 52;
            //                for (int i = cCount; i > 0; i--)
            //                {
            //                    dtRst.Columns.RemoveAt(dtRst.Columns.Count - 1);
            //                }
            //            }

            //            //List<string> colList = new List<string>();
            //            //foreach (DataRow dr in dtRst.Rows)
            //            //{
            //            //    for (int i = 0; i < dtRst.Columns.Count; i++)
            //            //    {
            //            //        if (dr[i].ToString().Length > 500)
            //            //        {
            //            //            if (!colList.Contains(dtRst.Columns[i].ColumnName))
            //            //            {
            //            //                colList.Add(dtRst.Columns[i].ColumnName);
            //            //            }
            //            //            //sb.AppendFormat("{0}-{1}", dtRst.Columns[i].ColumnName, dr[i].ToString().Length);
            //            //            //MessageBox.Show(dtRst.Columns[i].ColumnName + dr[i].ToString().Length);
            //            //        }
            //            //    }
            //            //}

            //            DbHelperSQL.SaveListDataBat(dtRst, "hotel");

            //            this.listBox2.Items.Add(item.ToString() + "导入数据完成");
            //            this.listBox2.Items.Add(sb.ToString());
            //        }
            //    }
            //}

            //DataTable dtRst = CsvParsingHelper.CsvToDataTable(@"D:\途牛对接\todev_1019途牛\todev_1019\hotel_20171019_1317_07.csv", true);

            //if (dtRst != null)
            //{
            //    MessageBox.Show(dtRst.Rows.Count.ToString());

            //    StringBuilder sb = new StringBuilder();

            //    if (dtRst.Columns.Count > 52)
            //    {
            //        var cCount = dtRst.Columns.Count - 52;
            //        for (int i = cCount; i > 0; i--)
            //        {
            //            dtRst.Columns.RemoveAt(dtRst.Columns.Count - 1);
            //        }
            //    }

            //DataTable dtS = DbHelperSQL.Query("SELECT * FROM HOTEL").Tables[0];
            ////MessageBox.Show(obj.ToString());

            //for (int i = 0; i < dtRst.Columns.Count; i++)
            //{
            //    if (dtRst.Columns[i].ColumnName != dtS.Columns[i].ColumnName)
            //    {
            //        MessageBox.Show(dtRst.Columns[i].ColumnName + " | " + dtS.Columns[i].ColumnName);
            //    }
            //    //sb.AppendFormat("{0}|", dtRst.Columns[i].ColumnName);
            //}

            //foreach (DataRow dr in dtRst.Rows)
            //{
            //    for (int i = 0; i < dtRst.Columns.Count; i++)
            //    {
            //        if (dr[i].ToString().Length > 1000)
            //        {
            //            MessageBox.Show(dtRst.Columns[i].ColumnName+ dr[i].ToString().Length);
            //        }
            //    }
            //}

            //}

            //DbHelperSQL.SaveListDataBat(dtRst, "hotel");
            //Object obj = DbHelperSQL.GetSingle("SELECT COUNT(1) FROM HOTEL");

            //MessageBox.Show("执行完成！");

            #endregion

        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string[] fileNames = openFileDialog1.FileNames;
                if (fileNames != null && fileNames.Length > 0)
                {
                    this.listBox1.Items.Clear();

                    foreach (var s in fileNames)
                    {
                        this.listBox1.Items.Add(s);
                    }
                }
                else
                {
                    MessageBox.Show("请选择文件！");
                }
            }
        }

        private void HotelInfoImport()
        {
            if (this.listBox1.Items.Count == 0)
            {
                MessageBox.Show("请选择文件！");
            }
            else
            {
                foreach (var item in this.listBox1.Items)
                {
                    this.listBox2.Items.Add("正在处理：" + item.ToString());

                    DataTable dtRst =
                        CsvParsingHelper.CsvToDataTable(item.ToString(), true);

                    if (dtRst != null)
                    {
                        //MessageBox.Show(dtRst.Rows.Count.ToString());
                        this.listBox2.Items.Add(item.ToString() + "包含数据：" + dtRst.Rows.Count.ToString());

                        StringBuilder sb = new StringBuilder();

                        if (dtRst.Columns.Count > 52)
                        {
                            var cCount = dtRst.Columns.Count - 52;
                            for (int i = cCount; i > 0; i--)
                            {
                                dtRst.Columns.RemoveAt(dtRst.Columns.Count - 1);
                            }
                        }

                        DbHelperSQL.SaveListDataBat(dtRst, "hotel");

                        this.listBox2.Items.Add(item.ToString() + "导入数据完成");
                        this.listBox2.Items.Add(sb.ToString());
                    }
                }
            }
        }

        private void RoomImport()
        {
            if (this.listBox1.Items.Count == 0)
            {
                MessageBox.Show("请选择文件！");
            }
            else
            {
                foreach (var item in this.listBox1.Items)
                {
                    this.listBox2.Items.Add("正在处理：" + item.ToString());

                    DataTable dtRst =
                        CsvParsingHelper.CsvToDataTable(item.ToString(), true);

                    if (dtRst != null)
                    {
                        this.listBox2.Items.Add(item.ToString() + "包含数据：" + dtRst.Rows.Count.ToString());

                        StringBuilder sb = new StringBuilder();

                        if (dtRst.Columns.Count > 23)
                        {
                            var cCount = dtRst.Columns.Count - 23;
                            for (int i = cCount; i > 0; i--)
                            {
                                dtRst.Columns.RemoveAt(dtRst.Columns.Count - 1);
                            }
                        }

                        //DataTable dtS = DbHelperSQL.Query("SELECT * FROM HOTEL_ROOM").Tables[0];
                        //for (int i = 0; i < dtRst.Columns.Count; i++)
                        //{
                        //    if (dtRst.Columns[i].ColumnName != dtS.Columns[i].ColumnName)
                        //    {
                        //        MessageBox.Show(dtRst.Columns[i].ColumnName + " | " + dtS.Columns[i].ColumnName);
                        //    }
                        //}

                        DbHelperSQL.SaveListDataBat(dtRst, "hotel_room");

                        this.listBox2.Items.Add(item.ToString() + "导入数据完成");
                        this.listBox2.Items.Add(sb.ToString());
                    }
                }
            }
        }

        private void PicImport()
        {
            if (this.listBox1.Items.Count == 0)
            {
                MessageBox.Show("请选择文件！");
            }
            else
            {
                foreach (var item in this.listBox1.Items)
                {
                    this.listBox2.Items.Add("正在处理：" + item.ToString());

                    DataTable dtRst =
                        CsvParsingHelper.CsvToDataTable(item.ToString(), true);

                    if (dtRst != null)
                    {
                        //MessageBox.Show(dtRst.Rows.Count.ToString());
                        this.listBox2.Items.Add(item.ToString() + "包含数据：" + dtRst.Rows.Count.ToString());

                        StringBuilder sb = new StringBuilder();

                        if (dtRst.Columns.Count > 10)
                        {
                            var cCount = dtRst.Columns.Count - 10;
                            for (int i = cCount; i > 0; i--)
                            {
                                dtRst.Columns.RemoveAt(dtRst.Columns.Count - 1);
                            }
                        }

                        DbHelperSQL.SaveListDataBat(dtRst, "hotel_picture");

                        this.listBox2.Items.Add(item.ToString() + "导入数据完成");
                        this.listBox2.Items.Add(sb.ToString());

                    }
                }
            }
        }
    }
}
